﻿namespace WCBG.ToolsForUnity.Extension
{
    using System;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using UnityEngine;

    public static class StringExtension
    {
        public static Color ConvertToColor(this string str, char splitStr = ';')
        {
            char[] separator = new char[] { splitStr };
            string[] strArray = str.Split(separator);
            if (strArray.Length != 4)
            {
                return Color.white;
            }
            return new Color(strArray[0].ConvertToFloat() / 255f, strArray[1].ConvertToFloat() / 255f, strArray[2].ConvertToFloat() / 255f, strArray[3].ConvertToFloat() / 255f);
        }

        public static float ConvertToFloat(this string str) => 
            float.Parse(str);

        public static int ConvertToInt(this string str) => 
            int.Parse(str);

        public static Vector3 ConvertToVector3(this string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                char[] separator = new char[] { ',' };
                string[] strArray = str.Split(separator);
                if (strArray.Length == 3)
                {
                    return new Vector3(float.Parse(strArray[0]), float.Parse(strArray[1]), float.Parse(strArray[2]));
                }
            }
            return Vector3.zero;
        }

        public static string FiltrateSuffix(this string name)
        {
            int length = name.LastIndexOf(".");
            if (length > 0)
            {
                return name.Substring(0, length);
            }
            return name;
        }

        public static string GetPathName(this string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                int num = str.LastIndexOf("/");
                if (num > 0)
                {
                    return str.Substring(num + 1, (str.Length - num) - 1);
                }
            }
            return str;
        }
    }
}

